import type { Menu } from '@/models/menu'
import type { CommonResult, CommonPage } from '@/models/request'
import type { Resource } from '@/models/resource'
import type { Role, RolePageDto } from '@/models/role'
import request, { ContentTypeEnum } from '@/utils/request'

export function fetchList(params: RolePageDto) {
  return request<CommonPage<Role>>({
    url: '/role/list',
    method: 'get',
    params: params,
  })
}

export function createRole(data: Role) {
  return request<CommonResult>({
    url: '/role/create',
    method: 'post',
    data: data,
  })
}

export function updateRole(id: number, data: Role) {
  return request<CommonResult>({
    url: '/role/update/' + id,
    method: 'post',
    data: data,
  })
}

export function updateStatus(id: number, params: { status: number }) {
  return request<CommonResult>({
    url: '/role/updateStatus/' + id,
    method: 'post',
    params: params,
  })
}

export function deleteRole(data: { ids: string }) {
  return request<CommonResult>({
    url: '/role/delete',
    method: 'post',
    data: data,
    headers: {
      'Content-Type': ContentTypeEnum.form,
    },
  })
}

export function fetchAllRoleList() {
  return request<CommonResult<Role[]>>({
    url: '/role/listAll',
    method: 'get',
  })
}

export function listMenuByRole(roleId: number) {
  return request<CommonResult<Menu[]>>({
    url: '/role/listMenu/' + roleId,
    method: 'get',
  })
}

export function listResourceByRole(roleId: number) {
  return request<CommonResult<Resource[]>>({
    url: '/role/listResource/' + roleId,
    method: 'get',
  })
}

export function allocMenu(data: { menuIds: string; roleId: number }) {
  return request<CommonResult>({
    url: '/role/allocMenu',
    method: 'post',
    data: data,
    headers: {
      'Content-Type': ContentTypeEnum.form,
    },
  })
}

export function allocResource(data: { resourceIds: string; roleId: number }) {
  return request<CommonResult>({
    url: '/role/allocResource',
    method: 'post',
    data: data,
    headers: {
      'Content-Type': ContentTypeEnum.form,
    },
  })
}
